class Teacher < ActiveRecord::Base
  has_many :quantities
  has_many :choices

  #  导师的个人信息
  def self.select_teacher_info(stu_type)
    find_by_sql(["
        SELECT t.tname, t.tresume, q.stu_type
        FROM   teachers t, quantities q
        WHERE  t.id = q.tea_id
        AND    q.stu_type = ?
        ", stu_type ])
  end

  #  得到已完成双选的教师录取结果
  def self.get_teacher_result
    find_by_sql(["
        SELECT q.tea_id, q.stu_type, q.numbers
        FROM   quantities q,
        (
        SELECT c.tea_id, count(c.stu_id) AS count
        FROM   choices c
        WHERE  c.stu_status = '同意' AND c.tea_status = '同意'
        GROUP BY c.tea_id
        ) m
        WHERE q.tea_id = m.tea_id AND m.count = q.numbers
        "])
  end

  #  教师登录操作
  def teacher_login(name, password)
    Teacher.login(name,password)
  end
  def self.login(n, p)
    find(:first, :conditions => ["tname = ? AND tpassword = ?", n, p])
  end
end
